<?php

Class MenuModel extends Model {

	/**
	 * 自动验证规则
	 * @var array
	 */
	protected $_validate = array(
		array('url','require' , '菜单URL必须填写！'),
	);

	/**
	 * 自动填充规则
	 * @var array
	 */
	protected $_auto = array(
		array('status' , 0 ,Model:: MODEL_INSERT),
	);


	/**
	 * 添加菜单
	 * @param array $post 添加数据
	 * @return bool 
	 */
	public function addMenu($post) {
		$data = $this->create($post);
		if(!empty($data)) {
			if($this->add()) {
				return true;
			}else{
				$this->error = '添加失败！';
				return false;
			}
		}else{
			$this->error = '数据不完整！'; 
			return false;
		}
	}
	/**
	 * 编辑菜单
	 * @param  array $post [description]
	 * @return [type]       [description]
	 */
	public function editMenu($post) {
		$data = $this->create($post);
		if(!empty($post)) {
			if($this->save()) {
				return true;
			}else{
				$this->error = '修改失败！';
				return false;
			}

		}else{
			$this->error = '数据不完整！'; 
			return false;
		}
	}

	/**
	 * 获取单条数据
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function getOne($id) {
		$data = $this->where(array('id' => $id))->find();
		if(empty($data)) {
			return array();
		}
		return $data;
	}

	/**
	 * 获取菜单
	 * @param  integer $parent [description]
	 * @return [type]          [description]
	 */
	public function getList($map = array()) {
		$data = $this->where($map)->select();
		if(empty($data)) {
			return array();
		}
		return $data;
	}

	/**
	 * [MenuByJson description]
	 */
	public function MenuByJson() {
		return $data = $this->get_tree(0);
	}

	/**
	 * [get_tree description]
	 * @return [type] [description]
	 */
	// public function get_tree($pid, $parent = "", $level = 1) {
	// 	$result = $this->admin_menu($pid);
	// 	$level ++;
	// 	if(is_array($result)) {
	// 		foreach ($result as $v) {
	// 			$id     = $v['id'];
	// 			$name   = ucwords($v['name']);
	// 			$urlArr = explode('/', $v['url']);
	// 			$group  = $urlArr[0];
	// 			$icon   =  empty($v['icon']) ? '' : $v['icon'];
	// 			$array = array(
	// 				"id"    => $id . $group,
	// 				'url'   => U($v['url']),
	// 				//"level" => $level,
	// 				'name'  => $name,
	// 				'icon'  => $icon,
	// 			);
	// 			$ret[$id . $group] = $array;
 //                $child = $this->get_tree($v['id'], $id, $level);
 //                if ($child && $level <= 3) {
 //                    $ret[$id . $group]['items'] = $child;
 //                }
	// 		}
	// 	}
	// 	return $ret;
	// }
	// 
	public function get_tree($pid, $parent = "", $level = 1) {
		$result = $this->admin_menu($pid);
		//$url    =  GROUP_NAME .'/'. MODULE_NAME . '/'.ACTION_NAME;
		$url    = 'Admin/Index/two';
		foreach ($result as $key => $value) {
			if($value['url'] == $url) {
				$result[$key]['active'] = "current";
			}
			
		}
		$child = $this->where(array('parent'=>2))->order(array("sort" => "ASC", 'id' => 'ASC'))->select();
		$group = $this->where(array('parent'=>2))->distinct(true)->field('group')->select();
		$group = array_column($group , 'group');
		$group = array_filter($group);

		foreach ($group as $key => $value) {
			$childen[$value] = array();
		}
		
		foreach ($child as $key => $value) {
			if($value['group']!== '') {
				$childen[$value['group']][] = $value;
			}
		}
		$return['top'] = $result;
		$return['child'] = $childen;
		return $return;
	}
	/**
	 * [admin_menu description]
	 * @return [type] [description]
	 */
	public function admin_menu($pid = 0) {

		$pid = intval($pid);
		$map = array(
			'parent' => $pid,
			'status' => 1,
		);
		$result = $this->where($map)->order(array("sort" => "ASC", 'id' => 'ASC'))->select();

		return $result;
	}
}